home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 June: System Software / Dev.CD Jun 97 SSW.toast / What's New? / Sample Code / Text / NeoTextBox97 / NTBDemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-23  |  11.5 KB  |  386 lines  |  [TEXT/CWIE]

  1. /*****************************************************************************************
  2.  
  3. NTBDemo.c - demo/test program for NeoTextBox
  4.  
  5. Written by Bryan K. Ressler (Beaker), 8/30/91
  6.  
  7. updated for the wonderful universe of PowerPC by Pete Gontier (Gurgle), 3/31/97
  8.  
  9. *****************************************************************************************/
  10.  
  11. #define BEAKER
  12.  
  13. /** INCLUDES ****************************************************************************/
  14.  
  15. #include "NTBDemo.h"        /* NTBDemo stuff */
  16. #include "Utilities.h"        /* Utility stuff */
  17. #include "NeoTextBox.h"        /* NeoTextBox module */
  18.  
  19. #ifndef __RESOURCES__
  20. #    include <Resources.h>
  21. #endif
  22.  
  23. #ifndef __FONTS__
  24. #    include <Fonts.h>
  25. #endif
  26.  
  27. #ifndef __TEXTUTILS__
  28. #    include <TextUtils.h>
  29. #endif
  30.  
  31. #ifndef __TOOLUTILS__
  32. #    include <ToolUtils.h>
  33. #endif
  34.  
  35. /** GLOBALS *****************************************************************************/
  36.  
  37.         short        gJustRadio;        /* Currently-set justification radio button */
  38.         short        gFontRadio;        /* Currently-set font radio button */
  39.  
  40. /** STATICS ****************************************************************************/
  41.  
  42. static    SysEnvRec    gEnvironment;    /* Our operating environment */
  43. static    UInt8        gsBlankStr[1];    /* Blank string */
  44. static    short        gsTextRadio;    /* Currently-set text radio button */
  45. static    Boolean        gsVariable;        /* State of "Variable line height" checkbox */
  46. static    short        gsNumLines;        /* The number of line in the wrapped text total */
  47. static    short        gsEndY;            /* The endingY from NeoTextBox item */
  48. static    short        gsLHUsed;        /* The line height used based on the given text spec */
  49. static    Str255        gsInfoStr;        /* The string displayed in the info item */
  50.  
  51. /****************************************************************************************/
  52. static pascal void UpdateParms(DialogPtr dialog)
  53. {
  54.     UInt8 convBuffer1[40],convBuffer2[40],convBuffer3[40];
  55.     
  56.     NumToString(gsEndY,convBuffer1);
  57.     NumToString(gsLHUsed,convBuffer2);
  58.     NumToString(gsNumLines,convBuffer3);
  59.     ParamText(convBuffer1,convBuffer2,convBuffer3,gsBlankStr);
  60.     
  61.     InvalItem(dialog,kValuesItem,0,0);
  62. }
  63.  
  64. /****************************************************************************************/
  65. static pascal void NTBItem(WindowPtr theWindow,short itemNum)
  66. {
  67.     short    aType;
  68.     Rect    theBox;
  69.     Handle    aHandle,text;
  70.     long    textLen;
  71.     short    just,lineHeight;
  72.     Boolean    oldPreserve,oldPreferred;
  73.     short    oldLH,oldNL,oldEY;
  74.  
  75.     GetDialogItem(theWindow,itemNum,&aType,&aHandle,&theBox);
  76.     PenNormal(); PenPat(&qd.gray);
  77.     ForeColor(blackColor);
  78.     FrameRect(&theBox);
  79.     PenNormal();
  80.     
  81.     InsetRect(&theBox,4,4);
  82.     EraseRect(&theBox);
  83.     
  84.     text = GetResource('TEXT',(gsTextRadio == kShannonText) ? kShannonID : kBarkerID);
  85.     if (text) {
  86.         oldLH = gsLHUsed;                    /* Remember these so we can check for diffs */
  87.         oldNL = gsNumLines;
  88.         oldEY = gsEndY;
  89.         
  90.         just = RadioToJust();                /* Find out what justification to use */
  91.         textLen = GetHandleSize(text);        /* Determine text length */
  92.         HLock(text);                        /* Lock text - NeoTextBox may move memory */
  93.         
  94.         TextParms(kSave);                    /* Save current port text parameters */
  95.         
  96.         TextFont(RadioToFont());            /* Set up the port's text settings */
  97.         TextFace(0);
  98.         TextSize(GetDefFontSize());
  99.         TextMode(srcOr);
  100.         
  101.         oldPreferred = GetOutlinePreferred();    /* Save old, prefer TrueType */
  102.         SetOutlinePreferred(true);
  103.         
  104.         if (gsVariable) {
  105.             oldPreserve = GetPreserveGlyph();    /* Save old, preserve glyphs */
  106.             SetPreserveGlyph(true);
  107.         }
  108.         
  109.         lineHeight = (gsVariable) ? -1 : 0;
  110.         
  111.         /* This is the fancy-schmancy way, see InfoItem for a simple example */
  112.         gsNumLines = NeoTextBox(*text,textLen,&theBox,just,lineHeight,&gsEndY,&gsLHUsed);
  113.         
  114.         SetOutlinePreferred(oldPreferred);
  115.         if (gsVariable)
  116.             SetPreserveGlyph(oldPreserve);
  117.         
  118.         TextParms(kRestore);                /* Restore the port's old text parameters */
  119.         
  120.         if (gsEndY <= theBox.bottom) {
  121.             PenPat(&qd.ltGray);                /* Draw a light gray line below the text */
  122.             PenMode(patOr);
  123.             ForeColor(blueColor);
  124.             MoveTo(theBox.left,gsEndY);
  125.             LineTo(theBox.right - 1,gsEndY);
  126.             ForeColor(blackColor);
  127.             PenNormal();
  128.         }
  129.         
  130.         if (gsLHUsed != oldLH || gsNumLines != oldNL || gsEndY != oldEY)
  131.             UpdateParms(theWindow);            /* Redraw the stats if they've changed */
  132.     }
  133. }
  134.  
  135. /****************************************************************************************/
  136. static pascal void NTBInfoItem(WindowPtr theWindow,short itemNum)
  137. {
  138.     short    aType;
  139.     Rect    theBox;
  140.     Handle    aHandle;
  141.  
  142.     GetDialogItem(theWindow,itemNum,&aType,&aHandle,&theBox);
  143.     ForeColor(blackColor);
  144.     
  145.     EraseRect(&theBox);
  146.     
  147.     TextParms(kSave);                    /* Save current port text parameters */
  148.         
  149.     TextFont(GetAppFont());            /* Set up the port's text settings */
  150.     TextFace(0);
  151.     TextSize(9);
  152.     TextMode(srcOr);
  153.     
  154.     /* This is the simple, TextBox-like way to call NeoTextBox */
  155.     NeoTextBox(gsInfoStr + 1,*gsInfoStr,&theBox,GetSysDirection(),0,nil,nil);
  156.     
  157.     TextParms(kRestore);                /* Restore the port's old text parameters */
  158. }
  159.  
  160. /****************************************************************************************/
  161. static pascal void RedrawInfo(DialogPtr dialog)
  162. {
  163.     InvalItem(dialog,kInfoItem,0,0);
  164.     BeginUpdate(dialog);
  165.     UpdateDialog(dialog,dialog->visRgn);
  166.     EndUpdate(dialog);
  167. }
  168.  
  169. /****************************************************************************************/
  170. static pascal void NTBPerformance(DialogPtr dialog)
  171. {
  172.     UInt8    buf1[40],buf2[40],buf3[40],buf4[40];
  173.     Handle    testTextHdl;
  174.     char    *testText;
  175.     long    testLen;
  176.     Rect    box;
  177.     short    i,just,percentFaster;
  178.     long    startTime,endTime,textBoxTime,neoTextBoxTime;
  179.     Fixed    factor;
  180.     Boolean    goForIt = true;
  181.     
  182.     just = RadioToJust();                        /* Set up justification */
  183.     testTextHdl = GetResource('TEXT',kTestTextID);
  184.     
  185.     if (just == ntbJustFull) {
  186.         goForIt = false;
  187.         StopAlert(kNoCanDoAlrtID,nil);
  188.     }
  189.     
  190.     if (goForIt) {
  191.         NumToString(kTestIterations,buf1);    /* Put stats dialog up */
  192.         ParamText(buf1,gsBlankStr,gsBlankStr,gsBlankStr);
  193.         goForIt = (NoteAlert(kDoPerfAlrtID,nil) == kOkayButton);
  194.     }
  195.     
  196.     if (goForIt && testTextHdl) {
  197.         testLen = GetHandleSize(testTextHdl);
  198.         HLock(testTextHdl);
  199.         testText = *testTextHdl;
  200.         
  201.         GetItemRect(dialog,kTextArea,&box);
  202.         InsetRect(&box,4,4);
  203.         
  204.         GetIndString(gsInfoStr,kStringsID,kCachingPass);
  205.         RedrawInfo(dialog);
  206.         TextParms(kSave);                            /* Save text parameters */
  207.         TextFont(RadioToFont());                    /* Set up the port's text settings */
  208.         TextFace(0);
  209.         TextSize(GetDefFontSize());
  210.         TextMode(srcOr);
  211.         TETextBox(testText,testLen,&box,just);
  212.         EraseRect(&box);
  213.         NeoTextBox(testText,testLen,&box,just,0,nil,nil);
  214.         TextParms(kRestore);                        /* Restore port's text settings */
  215.         
  216.         GetIndString(gsInfoStr,kStringsID,kTestingTextBox);
  217.         RedrawInfo(dialog);
  218.         TextParms(kSave);                            /* Save text parameters */
  219.         TextFont(RadioToFont());                    /* Set up the port's text settings */
  220.         TextFace(0);
  221.         TextSize(GetDefFontSize());
  222.         TextMode(srcOr);
  223.         startTime = TickCount();
  224.         for (i = 0; i < kTestIterations; i++)
  225.             TETextBox(testText,testLen,&box,just);
  226.         endTime = TickCount();
  227.         textBoxTime = endTime - startTime;
  228.         TextParms(kRestore);                        /* Restore port's text settings */
  229.         
  230.         GetIndString(gsInfoStr,kStringsID,kTestingNeoTextBox);
  231.         RedrawInfo(dialog);
  232.         TextParms(kSave);                            /* Save text parameters */
  233.         TextFont(RadioToFont());                    /* Set up the port's text settings */
  234.         TextFace(0);
  235.         TextSize(GetDefFontSize());
  236.         TextMode(srcOr);
  237.         startTime = TickCount();
  238.         for (i = 0; i < kTestIterations; i++) {
  239.             EraseRect(&box);
  240.             NeoTextBox(testText,testLen,&box,just,0,nil,nil);
  241.         }
  242.         endTime = TickCount();
  243.         neoTextBoxTime = endTime - startTime;
  244.         TextParms(kRestore);                        /* Restore port's text settings */
  245.         
  246.         HUnlock(testTextHdl);
  247.             
  248.         NumToString(kTestIterations,buf1);    /* Put stats dialog up */
  249.         NumToString(textBoxTime,buf2);
  250.         NumToString(neoTextBoxTime,buf3);
  251.         factor = FixRatio(textBoxTime,neoTextBoxTime) - Long2Fix(1);
  252.         percentFaster = FixRound(FixMul(factor,Long2Fix(100)));
  253.         NumToString(percentFaster,buf4);
  254.         ParamText(buf1,buf2,buf3,buf4);
  255.         NoteAlert(kPerfAlrtID,nil);
  256.                 
  257.         GetIndString(gsInfoStr,kStringsID,kStdInfo);    /* Restore info item text */
  258.         InvalItem(dialog,kInfoItem,0,0);
  259.         InvalItem(dialog,kTextArea,4,4);
  260.     }
  261.  
  262.     UpdateParms(dialog);
  263. }
  264.  
  265. /****************************************************************************************/
  266. static pascal void SetupDialog(DialogPtr testDialog)
  267. {
  268.     short    i;
  269.     
  270.     gJustRadio = JustToRadio(GetSysDirection());            /* Default to system justification */
  271.     gsVariable = false;                                /* Default to default line height */
  272.     gFontRadio = kAppFont;                            /* Default to application font */
  273.     gsTextRadio = kShannonText;                        /* Default to Shannon text */
  274.     
  275.     GetIndString(gsInfoStr,kStringsID,kStdInfo);    /* Set up info item */
  276.  
  277.     SetValue(testDialog,gJustRadio,1);                /* Set initial values */
  278.     SetValue(testDialog,kVariableHeight,gsVariable ? 1 : 0);
  279.     SetValue(testDialog,gFontRadio,1);
  280.     SetValue(testDialog,gsTextRadio,1);
  281.     
  282.     for (i = kBoxOne; i <= kLastBox; i++)
  283.         UserItem(testDialog,i,BoxItem);                /* Install box user items */
  284.     for (i = kDividerOne; i <= kLastDivider; i++)
  285.         UserItem(testDialog,i,GrayBoxItem);            /* Install divider user items */
  286.     UserItem(testDialog,kTextArea,NTBItem);            /* Install the main text item */
  287.     UserItem(testDialog,kInfoItem,NTBInfoItem);        /* Install the information item */
  288.     
  289.     NTBItem(testDialog,kTextArea);                    /* Draw (to set Y/line height vars) */
  290.  
  291.     ShowWindow(testDialog);                            /* Put it on the screen */
  292. }
  293.  
  294. /****************************************************************************************/
  295. static pascal void PoseDialog(DialogPtr testDialog)
  296. {
  297.     short    itemHit;
  298.  
  299.     do {
  300.         ModalDialog(nil,&itemHit);
  301.         switch (itemHit) {
  302.             case kPerfButton:
  303.                 NTBPerformance(testDialog);
  304.                 break;
  305.             case kJustLeft:                        /* Justification cluster */
  306.             case kJustCenter:
  307.             case kJustRight:
  308.             case kJustFull:
  309.                 if (gJustRadio != itemHit) {
  310.                     SetValue(testDialog,gJustRadio,0);
  311.                     gJustRadio = itemHit;
  312.                     SetValue(testDialog,gJustRadio,1);
  313.                     InvalItem(testDialog,kTextArea,4,4);
  314.                 }
  315.                 break;
  316.             case kVariableHeight:                /* Variable height checkbox */
  317.                 gsVariable = !gsVariable;
  318.                 SetValue(testDialog,kVariableHeight,gsVariable ? 1 : 0);
  319.                 InvalItem(testDialog,kTextArea,4,4);
  320.                 break;
  321.             case kAppFont:                        /* Font cluster */
  322.             case kTimes:
  323.             case kHelvetica:
  324.                 if (gFontRadio != itemHit) {
  325.                     SetValue(testDialog,gFontRadio,0);
  326.                     gFontRadio = itemHit;
  327.                     SetValue(testDialog,gFontRadio,1);
  328.                     InvalItem(testDialog,kTextArea,4,4);
  329.                 }
  330.                 break;
  331.             case kShannonText:                    /* Test cluster */
  332.             case kLiterature:
  333.                 if (gsTextRadio != itemHit) {
  334.                     SetValue(testDialog,gsTextRadio,0);
  335.                     gsTextRadio = itemHit;
  336.                     SetValue(testDialog,gsTextRadio,1);
  337.                     InvalItem(testDialog,kTextArea,4,4);
  338.                 }
  339.                 break;
  340.             default:
  341.                 break;
  342.         }
  343.     } while (itemHit != kQuitButton);
  344. }
  345.  
  346. /****************************************************************************************/
  347. static pascal void DemoNTB(void)
  348. {
  349.     GrafPtr        savePort;
  350.     DialogPtr    testDialog;
  351.     
  352.     GetPort(&savePort);
  353.     testDialog = GetNewDialog(kTestDlgID,nil,(WindowPtr)-1);
  354.     if (testDialog) {
  355.         SetPort(testDialog);
  356.         SetupDialog(testDialog);
  357.         PoseDialog(testDialog);
  358.         DisposeDialog(testDialog);
  359.     }
  360.     SetPort(savePort);
  361. }
  362.  
  363. /****************************************************************************************/
  364. static pascal Boolean Initialize(void)
  365. {
  366.     OSErr    err;
  367.     
  368.     InitGraf(&qd.thePort);            /* The usual Mac inits */
  369.     InitFonts();
  370.     InitWindows();
  371.     TEInit();
  372.     InitDialogs(nil);
  373.     InitCursor();
  374.     
  375.     *gsBlankStr = 0;    
  376.     err = SysEnvirons(2,&gEnvironment);
  377.     return((err || gEnvironment.systemVersion < 0x0600) ? false : true);
  378. }
  379.  
  380. /****************************************************************************************/
  381. void main(void)
  382. {
  383.     if (Initialize())
  384.         DemoNTB();
  385. }
  386.